<script setup>
/**
 * 首页视图：
 * - 展示宣传横幅、当季推荐、花艺小知识
 * - 从 dataService 读取花卉与分类数据
 * - 可跳转到花卉详情
 */
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getFlowers, getCategories } from '../services/dataService';
import { isUserLoggedIn, getCurrentUser } from '../services/dataService';

const router = useRouter();
const flowers = ref([]);
const categories = ref([]);
const recommendedFlowers = ref([]);
const isLoggedIn = ref(false);
const currentUser = ref(null);

onMounted(() => {
  // 加载花卉数据
  flowers.value = getFlowers();
  categories.value = getCategories();
  
  // 获取推荐花卉（前6个）
  recommendedFlowers.value = flowers.value.slice(0, 6);
  
  // 检查登录状态
  isLoggedIn.value = isUserLoggedIn();
  currentUser.value = getCurrentUser();
});

// 跳转到花卉详情页
const navigateToDetail = (id) => {
  router.push(`/flower/${id}`);
};

// 跳转到分类页
const navigateToCategory = (category) => {
  // 这里简化处理，实际项目中可能需要跳转到专门的分类页面
  alert(`跳转到${category}分类`);
};
</script>

<template>
  <main class="home-container">
    <!-- 大型横幅广告 -->
    <section class="hero-banner">
      <div class="banner-content">
        <h1 class="banner-title">把春天，搬入生活里</h1>
        <p class="banner-subtitle">当季限定花束低至8折，满99元免配送费</p>
        <button class="banner-button">立即选购</button>
        <div class="banner-tag"></div>
      </div>
    </section>
    
    <!-- 快速分类 -->
    <section class="quick-categories">
      <div class="category-item">
        <div class="category-image">
          <img src="https://picsum.photos/id/152/200/200" alt="当季花" />
          <div class="category-overlay">
            <span class="category-number">34款</span>
          </div>
        </div>
        <span class="category-text">当季花</span>
        <span class="category-tag">#3A4F41</span>
      </div>
      
      <div class="category-item">
        <div class="category-image">
          <img src="https://picsum.photos/id/153/200/200" alt="去灰吸甲醛之选" />
          <div class="category-overlay">
            <span class="category-number">18款</span>
          </div>
        </div>
        <span class="category-text">去灰吸甲醛之选</span>
        <span class="category-tag">#6B7D77</span>
      </div>
      
      <div class="category-item">
        <div class="category-image">
          <img src="https://picsum.photos/id/154/200/200" alt="半加仑" />
          <div class="category-overlay">
            <span class="category-number">22款</span>
          </div>
        </div>
        <span class="category-text">半加仑</span>
        <span class="category-tag">#344F41</span>
      </div>
    </section>
    
    <!-- 花卉目录横幅 -->
    <section class="flower-catalog-banner">
      <div class="banner-content">
        <h1 class="banner-title">探索丰富多彩的</h1>
        <p class="banner-subtitle">发现各类花材，点缀您的美好生活</p>
        <div class="banner-tag">#E8C4A1</div>
      </div>
    </section>
    
    <!-- 当季推荐 -->
    <section class="seasonal-recommendations">
      <div class="section-header">
        <h2 class="section-title">当季推荐</h2>
        <a href="#" class="section-more">查看更多</a>
      </div>
      
      <div class="flower-grid">
        <div 
          v-for="flower in recommendedFlowers" 
          :key="flower.id"
          class="flower-card"
          @click="navigateToDetail(flower.id)"
        >
          <div class="flower-image-container">
            <img 
              :src="flower.image" 
              :alt="flower.name"
              class="flower-image"
              loading="lazy"
            />
          </div>
        </div>
      </div>
    </section>
    
    <!-- 花艺小知识 -->
    <section class="flower-knowledge">
      <div class="knowledge-content">
        <div class="knowledge-text">
          <h3 class="knowledge-title">花艺小知识</h3>
          <p class="knowledge-desc">场场花艺盛宴，体验鲜花的艺术魅力。花艺师现场教学，带你领略一个绿色的世界。</p>
        </div>
        <div class="knowledge-image">
          <img src="https://picsum.photos/id/155/400/200" alt="花艺小知识" />
        </div>
      </div>
    </section>
  </main>
</template>

<style scoped>
.home-container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
}

/* 大型横幅广告 */
.hero-banner {
  position: relative;
  width: 100%;
  height: 500px;
  /* 使用更可靠的背景图片URL */
  background-image: url('https://images.unsplash.com/photo-1524236348860-f44423143311?ixlib=rb-4.0.3&auto=format&fit=crop&w=1200&q=80');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-bottom: 40px;
  /* 确保背景完全不透明 */
  background-color: transparent;
  opacity: 1;
  z-index: 1;
}

/* 为内容添加更高的z-index确保可见 */
.banner-content {
  text-align: center;
  z-index: 2;
  position: relative;
}

.banner-tag {
  position: absolute;
  bottom: 20px;
  right: 30px;
  font-size: 14px;
  color: white;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.banner-content {
  text-align: center;
  z-index: 1;
}

.banner-title {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 15px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.banner-subtitle {
  font-size: 16px;
  margin-bottom: 25px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.banner-button {
  padding: 12px 28px;
  background-color: #8E8C41;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.banner-button:hover {
  background-color: #7A7835;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(142, 140, 65, 0.3);
}

/* 快速分类 */
.quick-categories {
  display: flex;
  justify-content: space-between;
  margin-bottom: 40px;
  padding: 0 20px;
  gap: 20px;
}

.category-item {
  text-align: center;
  width: 100%;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease;
}

.category-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.category-image {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.category-overlay {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.category-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.category-text {
  display: block;
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 12px 0 5px 0;
}

.category-tag {
  display: block;
  font-size: 14px;
  color: #999;
  margin-bottom: 12px;
}

.category-number {
  font-size: 12px;
  color: white;
}

/* 花卉目录横幅 */
.flower-catalog-banner {
  position: relative;
  width: 100%;
  height: 350px;
  background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&auto=format&fit=crop&w=1200&q=80');
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-bottom: 40px;
  overflow: hidden;
  background-color: #8E8C41;
}

.flower-catalog-banner::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  z-index: 1;
}

/* 当季推荐 */
.seasonal-recommendations {
  margin-bottom: 40px;
  padding: 0 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.section-more {
  color: #8E8C41;
  text-decoration: none;
  font-size: 14px;
}

.section-more:hover {
  text-decoration: underline;
}

.flower-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
}

.flower-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.flower-card:hover {
  transform: translateY(-5px);
}

.flower-image-container {
  width: 100%;
  height: 150px;
  overflow: hidden;
}

.flower-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.flower-card:hover .flower-image {
  transform: scale(1.05);
}

/* 花艺小知识 */
.flower-knowledge {
  margin-bottom: 40px;
  padding: 0 20px;
}

.knowledge-content {
  display: flex;
  background: #F9F7F2;
  border-radius: 12px;
  overflow: hidden;
  height: 250px;
}

.knowledge-text {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.knowledge-title {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 15px;
  color: #333;
}

.knowledge-desc {
  font-size: 16px;
  color: #666;
  line-height: 1.6;
}

.knowledge-image {
  width: 40%;
  min-width: 300px;
}

.knowledge-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .hero-banner {
    height: 300px;
  }
  
  .banner-title {
    font-size: 36px;
  }
  
  .quick-categories {
    flex-direction: column;
    gap: 20px;
  }
  
  .category-item {
    width: 100%;
  }
  
  .flower-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .knowledge-content {
    flex-direction: column;
    height: auto;
  }
  
  .knowledge-image {
    width: 100%;
    min-width: auto;
    height: 200px;
  }
}
</style>
